//-----------------------------------------------------------------------
// <copyright file="a_Products_Acc_DetDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_Products_Acc_DetDAOBase
    {
        #region Common methods
        public virtual a_Products_Acc_Det Createa_Products_Acc_DetFromReader(IDataReader reader)
        {
            a_Products_Acc_Det item = new a_Products_Acc_Det();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ProdID"))) item.ProdID = (int)reader["ProdID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Acc_ProdID"))) item.Acc_ProdID = (int)reader["Acc_ProdID"];
                if (!reader.IsDBNull(reader.GetOrdinal("StoreID"))) item.StoreID = (int)reader["StoreID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Products_Acc_Det methods
            
        public virtual void Createa_Products_Acc_Det(a_Products_Acc_Det a_Products_Acc_Det)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Acc_Det_ins");
                
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_Products_Acc_Det.ProdID);
                database.AddInParameter(dbCommand, "@Acc_ProdID", DbType.Int32, a_Products_Acc_Det.Acc_ProdID);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_Products_Acc_Det.StoreID);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Products_Acc_Det.DomainID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Products_Acc_Det methods
        
        public virtual void Updatea_Products_Acc_Det(a_Products_Acc_Det a_Products_Acc_Det)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Acc_Det_upd");            
                
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_Products_Acc_Det.ProdID);
                database.AddInParameter(dbCommand, "@Acc_ProdID", DbType.Int32, a_Products_Acc_Det.Acc_ProdID);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_Products_Acc_Det.StoreID);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Products_Acc_Det.DomainID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Products_Acc_Det methods
        public virtual void Deletea_Products_Acc_Det(int prodID, int acc_ProdID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Acc_Det_del");
                
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, prodID);
                database.AddInParameter(dbCommand, "@Acc_ProdID", DbType.Int32, acc_ProdID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Products_Acc_Det methods
        
        public virtual a_Products_Acc_Det Geta_Products_Acc_Det(int prodID, int acc_ProdID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Acc_Det_sel");
                
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, prodID);
                database.AddInParameter(dbCommand, "@Acc_ProdID", DbType.Int32, acc_ProdID);
                
                a_Products_Acc_Det a_Products_Acc_Det =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Products_Acc_Det = Createa_Products_Acc_DetFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Products_Acc_Det;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_Products_Acc_DetList methods
        public virtual a_Products_Acc_DetCollection Geta_Products_Acc_DetList(a_Products_Acc_DetColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Acc_Det_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_Products_Acc_DetCollection a_Products_Acc_DetCollection = new a_Products_Acc_DetCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Products_Acc_Det a_Products_Acc_Det = Createa_Products_Acc_DetFromReader(reader);
                        a_Products_Acc_DetCollection.Add(a_Products_Acc_Det);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Products_Acc_DetCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_Products_Acc_DetCollection Geta_Products_Acc_DetList(a_Products_Acc_DetColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_Products_Acc_DetList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}